require("../util.js");
require("../component/components.js");
require("../component/cityChoice.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#opinion",
    data:{
        number:"1",
        content:"",
        imgSrc:[],
        imgSrcList:[]
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.type=this.searchObject&&this.searchObject.type;
        //this.getChinaList();
    },
    methods:{
        //后退
        getBack: function () {
            location.href="/happyi/setUp";
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //图片
        getChange:function(index){
            var that=this;
            console.log("33333333333增加"+index);
            var fileinput=document.getElementById("fileinput"+index).files[0];
            var coverBtn=document.getElementById("coverBtn"+index).className;
            if(coverBtn=="opinion-icon"){
                that.imgSrc.splice(index,1,fileinput);
            }else{
                that.imgSrc.splice(index,0,fileinput);
                // if(that.type==1){
                //     that.imgSrc.splice(index,0,{"imgSrc":fileinput});
                // }else{
                //     that.imgSrc.splice(index,0,fileinput);
                // }
            }
            console.log(that.imgSrc)
            lrz(fileinput, function (results) {
                // 你需要的数据都在这里，可以以字符串的形式传送base64给服务端转存为图片。
                // that.results=results;
                console.log(results)
                if(coverBtn=="opinion-icon"){
                    that.imgSrcList.splice(index,1,results.base64);
                }else{
                    that.imgSrcList.splice(index,0,results.base64);
                    that.number++;
                }

                document.getElementById("coverImg"+index).src=results.base64;
                document.getElementById("coverBtn"+index).className="opinion-icon";
            });
        },
        deleteImg:function(index){
            var that=this;
            that.imgSrc.splice(index,1);
            that.imgSrcList.splice(index,1);
            document.getElementById("coverImg"+index).src="";
            document.getElementById("coverBtn"+index).className="";
            that.number--;
            console.log(that.imgSrc)
        },
        //提交反馈
        feedback:function(){
            var that=this;
            console.log(that.imgSrc.length)
            if(!that.content){
                vm.$refs.prompt.toast("请填写反馈意见！");
                return false;
            }
            if(that.imgSrc.length<1){
                vm.$refs.prompt.toast("请添加反馈图片！");
                return false;
            }

            var imgSrcList=that.imgSrc;
            var formData = new FormData();
            for(var i=0;i<imgSrcList.length;i++){
                formData.append("imgs",imgSrcList[i]);
            }
            formData.append("loginUid", userInfo.loginUid);
            formData.append("content", that.content);
            $.ajax({
                url:host+"/v1/user/feedback",
                type:'POST',
                data:formData,
                dataType:'json',
                async: false,
                cache: false,
                contentType: false, //不设置内容类型
                processData: false, //不处理数据
                traditional:true,
                success:function(data){
                    console.log(data);
                    if(data.code==0){
                        vm.$refs.prompt.toast("提交成功！");
                        that.getBack();
                    }
                },
                error:function(){
                    vm.$refs.prompt.toast("提交失败！");
                }
            });

        }
    },
    watch:{

    }
});
